import { request } from '../utils/request';

// 应用相关API
export interface ApplicationData {
  appId: string;
  appName: string;
  appDescription?: string;
  ownTeam?: string;
  ownCenter?: string;
  unitCount?: number;
  totalCodeLines?: number;
  avgCyclomaticComplexity?: number;
  duplicationPercentage?: number;
  createTime?: string;
  updateTime?: string;
}

export interface UnitMetricsData {
  unitId: string;
  appId?: string;
  appName?: string;
  teamName?: string;
  unitType?: string;
  totalFiles?: number;
  totalLines?: number;
  totalCodeLines?: number;
  totalCommentLines?: number;
  totalBlankLines?: number;
  lastScanTime?: string;
  mainLanguage?: string;
  technologyStack?: string;
  scanStatus?: string;
  elapsedSeconds?: number;
  clocVersion?: string;
  // PMD扫描相关字段
  avgCyclomaticComplexity?: number;
  totalCyclomaticComplexity?: number;
  maxCyclomaticComplexity?: number;
  highComplexityMethods?: number;
  veryHighComplexityMethods?: number;
  duplicationPercentage?: number;
  duplicatedLines?: number;
  duplicatedBlocks?: number;
  pmdVersion?: string;
  pmdScanTime?: string;
  pmdElapsedSeconds?: number;
  pmdScanStatus?: string;
  // 接口和仓库统计字段
  totalApiCount?: number;
  suspectedDeprecatedApiCount?: number;
  repoActivityLevel?: string;
  commitCountLastYear?: number;
  // 数据状态字段
  apmDataStatus?: string;
  gitDataStatus?: string;
  jarDataStatus?: string;
  saDataStatus?: string;
  // Git仓库相关字段
  gitRepoUrl?: string;
  defaultBranch?: string;
  currentBranch?: string;
  currentCommitId?: string;
  currentArtifactoryId?: string;
  codeUpdateTime?: string;
  branches?: string[];
}

// 分页查询应用汇总数据
export const getApplicationMetrics = (params: {
  current?: number;
  size?: number;
  appName?: string;
  appId?: string;
}) => {
  return request({ url: '/metrics/applications', method: 'get', params });
};

// 获取指定应用下的单元数据
export const getApplicationUnits = (appId: string) => {
  return request({ url: `/metrics/applications/${appId}/units`, method: 'get' });
};

// 获取单元度量数据(支持应用分组和分页)
export const getUnitMetrics = (params: {
  unitName?: string;
  appId?: string;
  unitId?: string;
  keyword?: string;
  activityLevel?: string;
  dataStatus?: string;
  current?: number;
  size?: number;
}) => {
  return request({ url: '/metrics/units', method: 'get', params });
};

// 创建应用
export const createApplication = (data: ApplicationData) => {
  return request({ url: '/metrics/applications', method: 'post', data });
};

// 更新应用
export const updateApplication = (appId: string, data: ApplicationData) => {
  return request({ url: `/metrics/applications/${appId}`, method: 'put', data });
};

// 删除应用
export const deleteApplication = (appId: string) => {
  return request({ url: `/metrics/applications/${appId}`, method: 'delete' });
};

// 获取应用详情
export const getApplication = (appId: string) => {
  return request({ url: `/metrics/applications/${appId}`, method: 'get' });
};